<?php

namespace app\admin\model\xc\dynamics;

use think\Model;


class Info extends Model
{





    // 表名
    protected $name = 'xc_dynamics_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_pinned_text',
        'is_locked_text',
        'create_time_text',
        'update_time_text',
        'is_approved_text'
    ];



    public function getIsPinnedList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }

    public function getIsLockedList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }

    public function getIsApprovedList()
    {
        return ['0' => __('待审核'), '1' => __('通过'), '2' => __('未通过')];
    }


    public function getIsPinnedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pinned']) ? $data['is_pinned'] : '');
        $list = $this->getIsPinnedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsLockedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_locked']) ? $data['is_locked'] : '');
        $list = $this->getIsLockedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsApprovedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_approved']) ? $data['is_approved'] : '');
        $list = $this->getIsApprovedList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function type()
    {
        return $this->belongsTo('app\admin\model\xc\dynamics\Type', 'type_id', 'id')->setEagerlyType(0);
    }
    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id')->setEagerlyType(0);
    }
}
